package com.shareware.common.utils;

import static android.Manifest.permission.CALL_PHONE;

import android.content.Intent;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;

import com.shareware.common.ApplicationKt;

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */
public final class PhoneUtils {

    private PhoneUtils() {
    }

    /**
     * 启动呼叫页，需要用户手动点击呼叫
     *
     * @param phoneNumber The phone number.
     */
    public static void dial(@NonNull final String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Uri.encode(phoneNumber)));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ApplicationKt.getContextGlobal().startActivity(intent);
    }

    /**
     * Make a phone call.
     * <p>Must hold {@code <uses-permission android:name="android.permission.CALL_PHONE" />}</p>
     *
     * @param phoneNumber The phone number.
     */
    @RequiresPermission(CALL_PHONE)
    public static void call(@NonNull final String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode(phoneNumber)));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ApplicationKt.getContextGlobal().startActivity(intent);
    }

    /**
     * Send sms.
     *
     * @param phoneNumber The phone number.
     * @param content     The content.
     */
    public static void sendSms(@NonNull final String phoneNumber, final String content) {
        Uri uri = Uri.parse("smsto:" + Uri.encode(phoneNumber));
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", content);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ApplicationKt.getContextGlobal().startActivity(intent);
    }
}
